package com.example.clxy.service.impl;

import com.example.clxy.dao.SpecialtyMapper;
import com.example.clxy.dao.UserMapper;
import com.example.clxy.dao.WlclassMapper;
import com.example.clxy.entity.User;
import com.example.clxy.entity.Wlclass;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.StandardResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.ClassControllerResult;
import com.example.clxy.result.business.GradeClassResult;
import com.example.clxy.result.business.GradeResult;
import com.example.clxy.result.business.PageResult.ClassPageResult;
import com.example.clxy.result.business.WlClassResult;
import com.example.clxy.service.ClassService;
import com.example.clxy.util.RespUtil;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author weichong
 * date 2020-08-03
 * @Description:
 */

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ClassServiceImpl implements ClassService {

    @Autowired
    private WlclassMapper wlclassMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    SpecialtyMapper specialtyMapper;

    @Override
    public StandardResult getAllGradeClass(){
        List<GradeResult> gradlist= wlclassMapper.getAllGrade();
        if(gradlist.size() > 0){
            for (GradeResult g:gradlist) {
                List<GradeClassResult> gradeClassResultList =  wlclassMapper.getClassByGrade(g.getGrade());
                g.setClassList(gradeClassResultList);
            }
        }
        return RespUtil.successStandardResult("查询成功",gradlist);
    }

    @Override
    public StandardResult getAllGrade(){
        List<GradeResult> gradlist= wlclassMapper.getAllGrade();
        List<String> list = new ArrayList<>();
        for (GradeResult g :gradlist) {
            list.add(g.getGrade());
        }
        return RespUtil.successStandardResult("查询成功",list);
    }


    @Override
    public PageResult<ClassPageResult> getClassList(String teacherid,String semesterid,String classname,String grade, int page, int pagesize){
        PageResult<ClassPageResult> res = new PageResult<>();
        int begin = (page-1) * pagesize;
        int end = pagesize;

        List<ClassPageResult> classList = wlclassMapper.getClassList(teacherid,semesterid,classname,grade,begin,end,pagesize);
        if(classList.size()>0){
            res.setTotal(classList.get(0).getCnt());
            res.setPageCount(classList.get(0).getPage());
            res.setRows(classList);
            res.setResult(WebRestResult.SUCCESS);
            res.setMessage("查询班级信息成功");
        }
        return res;
    }

    @Override
    public WebRestResult WlclassbindSpecialty(String wlclassid, String specialtyid) {
        WebRestResult result = new WebRestResult();
        if (specialtyMapper.SpecialtybindWlclass(specialtyid,wlclassid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("绑定成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("绑定失败！");
        }
        return result;
    }
    @Override
    public StandardResult getClassController(String classid){
        StandardResult res = new StandardResult();
        Wlclass wlclass = wlclassMapper.selectByPrimaryKey(classid);
        if(wlclass == null || wlclass.getFlag().equals("1")){
            return RespUtil.failStandardResult("班级信息不存在",null);
        }
        List<Object> userList=new ArrayList<>();
        User monitor = userMapper.selectByPrimaryKey( wlclass.getMonitorid());
        User study = userMapper.selectByPrimaryKey( wlclass.getStudyleaderid());
        if(monitor != null){
            ClassControllerResult classControllerResult = new ClassControllerResult();
            classControllerResult.setAccount(monitor.getAccount());
            classControllerResult.setName(monitor.getUsername());
            classControllerResult.setPhone(monitor.getPhone());
            classControllerResult.setUserid(monitor.getUserid());
            classControllerResult.setPosition("班长");
            userList.add(classControllerResult);
        }
        if(study != null){
            ClassControllerResult classControllerResult = new ClassControllerResult();
            classControllerResult.setAccount(study.getAccount());
            classControllerResult.setName(study.getUsername());
            classControllerResult.setPhone(study.getPhone());
            classControllerResult.setUserid(study.getUserid());
            classControllerResult.setPosition("学习委员");
            userList.add(classControllerResult);
        }
        return RespUtil.successStandardResult("班委信息查询成功",userList);
    }

    @Override
    public PageResult<ClassPageResult> getWlclassList(int page, int pagesize, String wlclassname, String grade) {
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        PageResult<ClassPageResult> result = new PageResult<>();

        List<ClassPageResult> list = wlclassMapper.getWlclassList(begin, end, pagesize, wlclassname, grade);
        if (list.size() > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取班级列表成功！");
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setTotal(list.get(0).getCnt());
        }
        return result;
    }

    @Override
    public WebRestResult addWlclass(Wlclass wlclass) {
        WebRestResult result = new WebRestResult();
        wlclass.setWlclassid(UUIDUtil.randomUUID());
        wlclass.setFlag("0");
        wlclass.setCreatedTime(new Date());
        wlclass.setUpdatedTime(new Date());
        if (wlclassMapper.insertSelective(wlclass) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增班级成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增班级失败！");
        }
        return result;
    }

    @Override
    public WebRestResult updateWlclass(Wlclass wlclass) {
        WebRestResult result = new WebRestResult();
        wlclass.setUpdatedTime(new Date());
        wlclassMapper.updateByPrimaryKeySelective(wlclass);
        result.setResult(WebRestResult.SUCCESS);
        result.setMessage("更新班级成功！");
        return result;
    }

    @Override
    public WebRestResult deleteWlclass(String wlclassid) {
        WebRestResult result = new WebRestResult();
        int count = wlclassMapper.getStudentNumByClass(wlclassid);
        if (count > 0) {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("该班级下还有"+count+"名学生！若要删除班级，请先删除学生");
            return result;
        }
        if (wlclassMapper.deleteWlclass(wlclassid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除班级成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除班级失败！");
        }
        return result;
    }

    @Override
    public WlClassResult getWlclassById(String wlclassid) {
        WlClassResult wlClassResult = wlclassMapper.getWlclassById(wlclassid);
        if (wlClassResult != null) {
            wlClassResult.setResult(WebRestResult.SUCCESS);
            wlClassResult.setMessage("根据id获取班级信息成功！");
        } else {
            wlClassResult.setResult(WebRestResult.FAILURE);
            wlClassResult.setMessage("没有该班级信息");
        }
        return wlClassResult;
    }

}
